package cn.bx.bid.ctrl;

import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.User;
import cn.bx.bid.entity.Userrole;
import cn.bx.bid.service.RoleService;
import cn.bx.bid.service.UserService;
import cn.bx.bid.util.AjaxResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;

@Controller
public class UserControler {
    @Resource
    UserService userService;
    @Resource
    RoleService roleService;

 //登录
 @PostMapping(value = "ulogin",produces = "application/json;charset=utf-8")
 @ResponseBody//映射json
 public AjaxResult userlogin(HttpServletRequest request, @RequestParam("username")String user, @RequestParam("password") String p){
     User u=this.userService.login(user,p);
     if (u != null){
         request.getSession().setAttribute("USER_LOGIN", u);
         return AjaxResult.success("msg",u);
     }
     return AjaxResult.error();
 }
    //管理员分页
    @RequestMapping(value = "usersearch", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Page<User> usersearch(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int limit, @RequestParam(name = "name", required = false) String name, @RequestParam(name = "start", required = false) Date start, @RequestParam(name = "end", required = false) Date end) {
        Page<User> page = userService.paging(pageNo, limit, name, start, end);
        return page;
    }

    //添加用户
    @PostMapping(value = "userAdd", produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult userAdd(@ModelAttribute("u") User user, HttpServletRequest req) {
        int r = userService.add(user);
        if (r>0){

            return AjaxResult.success("success");
        }
        return AjaxResult.error("error");
    }

//批量删除
    @RequestMapping(value = "batchDel",produces = "application/json;charset=utf-8")
    @ResponseBody
public AjaxResult batchDel(@RequestParam("ids[]") long[] ids) {

    int r = userService.batchDel(ids);
  if (r>0){
      return AjaxResult.success("success");
  }
   return AjaxResult.error("error");
}


    //验证用户名是否重复
    @RequestMapping(value = "checkUserNameRepeat",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult checkUserNameRepeat(@RequestParam(name="userName") String name, @RequestParam(name="id",required = false) Long id) {

        boolean repeat = this.userService.checkUserNameRepeat(id, name);
        if (repeat){
            return AjaxResult.error("重复");
        }
        return AjaxResult.success("成功");

    }

//获取值
@RequestMapping(value = "userGet",produces = "application/json;charset=utf-8")
@ResponseBody
public AjaxResult userGet(@RequestParam(name="id")long id) {
    User user = this.userService.get(id);
    return AjaxResult.success("msg",user);
}
//下拉框
@RequestMapping(value = "findRoles",produces = "application/json;charset=utf-8")
@ResponseBody
public List<Userrole> findRoles()
         {
    List<Userrole> li = roleService.findALL();
  // String str = JSON.toJSONStringWithDateFormat(li, "yyyy-MM-dd");
return li;
}
    //修改
    @RequestMapping(value = "userUpdate",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult userUpdate(@ModelAttribute("u") User user)
             {
       int r=this.userService.update(user);
       if (r>0){
           return AjaxResult.success("success");
       }
       return AjaxResult.error();
    }

    //用户修改密码
    @RequestMapping(value = "UpdatePass",produces = "application/json;charset=utf-8")
    @ResponseBody
   public AjaxResult UpdatePass(String password,long id){

        int b = userService.updatepass(password,id);
        if(b>0){
            return AjaxResult.success("修改密码成功");
        }else {
            return AjaxResult.error("修改密码失败");
        }
    }

//删除用户
@RequestMapping(value = "delete",produces = "application/json;charset=utf-8")
@ResponseBody
public AjaxResult delete(@RequestParam(name = "id",required = false) long id){
        int i=this.userService.delete(id);
        if (i>0){
            return AjaxResult.success("success");
        }
        return AjaxResult.error();
}

    //处理日期格式
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值
    }
}
